#ifdef __MMI_DSM_NEW__
#define __NEWSIMULATOR 

#include "MMI_Include.h"
#include "MMI_features.h" 
#include "stdC.h"
#include "PixtelDataTypes.h"

#include "lcd_if.h"							/* LCD layer enable flag */
#include "lcd_sw_rnd.h"						/* for LCD size */
#include "conversions.h"
#include "lcd_sw_inc.h"
#include "MMI_features_camera.h"

#if(MTK_VERSION >=0x09B0952)
#include "GlobalConstants.h"
#include "mmi_frm_events_gprot.h"
#endif
#include "FontRes.h"
#include "TimerEvents.h"
#if (MTK_VERSION < 0x0936 )//huangsunbo 20100112
#include "EventsGprot.h"
#endif
#include "gpioInc.h"
#include "Mdi_audio.h"
#include "Mdi_datatype.h"
#include "FileManagerGProt.h"
#include "Fs_type.h"
#include "Fs_func.h"
#if (MTK_VERSION < 0x09B0952)
#include "SMSApi.h"
#endif
#include "Ems.h"
#include "DateTimeGprot.h"
#include "DateTimeType.h"
//#include "SimDetectionDef.h"
#include "SmsGuiInterfaceType.h"
#include "GlobalConstants.h"
#include "CommonScreens.h"
#include "customer_ps_inc.h"
#include "mmi_msg_context.h"
#include "wgui_categories_inputs.h"
#include "PixtelDataTypes.h"
#include "FileMgr.h"
#include "Gui_data_types.h"
#include "AudioPlayerDef.h"
#include "AudioPlayerType.h"
#include "aud_id3_parser.h"
#include "Gdi_include.h"
#include "Gdi_datatype.h"
#include "Gdi_image_bmp.h"
#include "app_ltlcom.h"
#include "stack_ltlcom.h"
#include "SSCStringHandle.h"
#include "GlobalMenuItems.h"
#include "MessagesResourceData.h"
#include "med_global.h"
#include "APP2SOC_STRUCT.H"
#include "ProtocolEvents.h"
#include "pixcomfontengine.h"
#include "l1audio.h"
#include "device.h"
#include "resource_audio.h"
#include "fat_fs.h"
#include "nvram_enums.h"
#include "nvram_struct.h"
#include "nvram_user_defs.h"
#include "nvram_data_items.h"
#include "custom_nvram_editor_data_item.h"
#include "custom_equipment.h"
#include "ScrMemMgrGprot.h"
#include "med_global.h"
#include "aud_defs.h"
#include "med_struct.h"
#include "med_api.h"
#include "med_context.h"
#include "lcd_sw_rnd.h"
#include "mdi_camera.h"
#include "cameraapp.h"
#include "mdi_camera.h"
#include "gdi_include.h"
#include "med_main.h"
#include "mdi_audio.h"
#include "mrp_include.h"

#ifdef __MED_IN_ASM__
#define med_alloc_ext_mem(size) applib_asm_alloc_anonymous_nc(size)
#define med_alloc_ext_mem_cacheable(size) applib_asm_alloc_anonymous(size)
#define med_free_ext_mem(ptr) applib_asm_free_anonymous(*ptr)
#endif

#define APM_SUPPORT  ( ( defined(MT6225) || defined(MT6228) || defined(MT6229) || defined(MT6230) || defined(MT6268T) || defined(MT6235) || defined(MT6238) || defined(MT6235B) || defined(MT6239) || defined(MT6268A) || defined(MT6268) || defined(MT6253T) || defined(MT6253) ) && !defined( MED_MODEM ) && !defined( MED_NOT_PRESENT ) )

#undef __NEWSIMULATOR
#if (MTK_VERSION >= 0x0736)//0x0808
typedef enum
{
    QUALITY_LOW,
    QUALITY_HIGH,
    NO_OF_QUALITY
}SoundRecorderQualityEnum;
#endif

#if(MTK_VERSION > 0x0848)
	#ifndef MDI_AUD_VOL_EX_MUTE
	#define MDI_AUD_VOL_EX_MUTE(x)  (x)
	#endif
#else
	#ifndef MDI_AUD_VOL_EX_MUTE
	   #define MDI_AUD_VOL_EX_MUTE(x)  ((x) | 0xC0)
	#else
	   #undef MDI_AUD_VOL_EX_MUTE
	   #define MDI_AUD_VOL_EX_MUTE(x)  ((x) | 0xC0)
	#endif
#endif

#ifndef MDI_AUD_PTH_EX
#if defined(__BTVCSR_HCI_BCHS__) && defined(__MMI_A2DP_SUPPORT__)
#define MDI_AUD_PTH_EX(x) ((x) | MDI_DEVICE_BT_HEADSET)
#else
#define MDI_AUD_PTH_EX(x)  (x)
#endif
#endif

#define DSM_MAX_VOL     5

int32 dsmReadAddr = 0;
int32 dsmWriteAddr = 0;
unsigned char  dsmLoopPCM = 0;

#if APM_SUPPORT //( defined(MT6225))
static U8  dsm_is_apm_alloc = 0;
#endif

static mr_audio_ctrl_t gDsmAudio = {0};
static U32 dsm_restore_const_seconds = 0;
static U32 dsm_restore_play_seconds =0;
static char s_dsm_vr_stream_started = FALSE;

static void dsm_play_callback( mdi_result result );
static void dsm_pause_callback( mdi_result result );
static void dsm_resume_callback( mdi_result result );
static void dsm_play_audio(void);
static void dsm_pause_audio(void);
static void dsm_stop_audio(void);
static void dsm_resume_audio(void);
static S32 dsm_play_action_without_handle_result(void);
static void dsm_audply_count_timer(void);
static S32 dsm_restore_action_without_handle_result(void);
static void dsm_start_record_play_seconds(void);
static void dsm_update_play_seconds(void);
static void dsm_clear_play_seconds(void);
static void dsm_init_audio(void);
static S32 dsm_get_audio_time(void);
static int32 dsm_audio_load(mr_audio_load_info_t *media, BOOL is_file, int32 media_type);
static S32 dsm_get_play_seconds(void);
static void dsm_set_bgplay_vol(void);
static S32 dsm_get_play_milliseconds(void);
static void dsm_unInit_camera(void);
static void dsm_entry_preview(void);
static void dsm_camera_preview_set_blt_layer(BOOL is_previewing);
static void dsm_camera_preview_start(void);
static S32 dsm_camera_get_zoom_total_level_cnt(void);
static U16 dsm_camera_preview_get_zoom_factor(void);


#if APM_SUPPORT //( defined(MT6225) )
extern unsigned char *APM_Allocate_Buffer(unsigned int bufSize);
extern void APM_Release_Buffer(void);
#endif 

extern pBOOL mmi_usb_is_in_mass_storage_mode(void);
static void dsmEntryCamera(void);

#if(MTK_VERSION > 0x05c)
#define DSM_PLATFORM_MAX_VOL     (MDI_AUD_VOL_EX_NUM)
#else
#define DSM_PLATFORM_MAX_VOL     (MAX_VOL_LEVEL)
#endif

#if  ! (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))//huangsunbo 20100121 for 6223
extern  mr_camera_ctrl_t gDsmCameraReq;
//extern camera_context_struct g_camera_cntx;
#endif

/* this feature add for __MMI_DSM_NEW_JSKY__, later will effect all */
#if defined(__MMI_SCREEN_ROTATE__)
extern U8 current_screen_rotation;
#endif

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void mr_media_audio_reset(void)
{
	dsmLoopPCM = 0;	
}


int32 mr_media_audio_set_vol(int32 vol)
{
	int32 temp;
	
	temp = vol > DSM_MAX_VOL?DSM_MAX_VOL:vol;

	gDsmAudio.volume = (temp*(DSM_PLATFORM_MAX_VOL-1))/DSM_MAX_VOL;
	
	if((gDsmAudio.state == MR_MEDIA_PLAYING) ||(gDsmAudio.state == MR_MEDIA_PAUSED)||(mdi_audio_get_state() ==1))
		mdi_audio_set_volume(VOL_TYPE_MEDIA, MDI_AUD_VOL_EX_MUTE(gDsmAudio.volume));

	return MR_SUCCESS;
}


static void dsm_set_bgplay_vol(void)
{
	U8 volume = mr_media_audio_get_vol();
	mdi_audio_set_headset_mode_output_path(MDI_AUD_PTH_EX(MDI_DEVICE_SPEAKER2));
	mdi_audio_set_volume(VOL_TYPE_MEDIA,MDI_AUD_VOL_EX_MUTE(volume));
}


int32 mr_media_audio_get_vol(void)
{
	return gDsmAudio.volume;
}


static void dsm_init_audio(void)
{	
#ifdef __MMI_FM_RADIO__
	//mmi_fmrdo_power_on( FALSE ); /*2009-04-14 */
#endif // __MMI_FM_RADIO__
	memset(&gDsmAudio,0,sizeof(mr_audio_ctrl_t));

	gDsmAudio.state = MR_MEDIA_INITED;
	gDsmAudio.volume = (4*(DSM_PLATFORM_MAX_VOL-1))/DSM_MAX_VOL;
	dsm_clear_play_seconds();
}


int32 mr_media_audio_get_state(void)
{
	return (gDsmAudio.state +MR_PLAT_VALUE_BASE);
}


static S32 dsm_get_audio_time(void)
{
	U32 time = 0;
	
	if(gDsmAudio.state < MR_MEDIA_LOADED)
		return -1;
	
	if(gDsmAudio.load_type == MR_MEDIA_FILE_LOAD)
	{
		if(mdi_audio_get_duration(gDsmAudio.filename,&time) == MDI_AUDIO_SUCCESS)
		{
#if (MTK_VERSION == 0x05c)
			if(gDsmAudio.media_format == MDI_FORMAT_M4A)
				return time/2000;
			else
#endif				
				return time/1000;
		}
		else
		{
			return -1;
		}
	}
	else
	{
		return -1;
	}
}



int32 mr_media_audio_get_total_time(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	S32 time;
	time = dsm_get_audio_time();
	
	if(time == -1)
	{
		return MR_FAILED;
	}
	else
	{
		g_mr_common_rsp.p1 = time;
		*output = (uint8 *)&g_mr_common_rsp;
		*output_len = sizeof(int32);
		return MR_SUCCESS;
	}
}


int32 mr_media_init_device(void)
{
	dsm_init_audio();
	return MR_SUCCESS;
}


static int32 dsm_audio_load(mr_audio_load_info_t *media,BOOL is_file,int32 media_type)
{
	
	if((gDsmAudio.state > MR_MEDIA_LOADED))
		return MR_FAILED;
	
	switch(media_type)
	{
		case ACI_MIDI_DEVICE:
			gDsmAudio.media_format = MDI_FORMAT_SMF;
			break;
		case ACI_WAVE_DEVICE:
			gDsmAudio.media_format = MDI_FORMAT_WAV;
			break;
		case ACI_MP3_DEVICE:
			gDsmAudio.media_format = MDI_FORMAT_DAF;
			break;
		case ACI_AMR_DEVICE:
			gDsmAudio.media_format = MDI_FORMAT_AMR;
			break;
		case ACI_AMR_WB_DEVICE:
			gDsmAudio.media_format = MDI_FORMAT_AMR_WB;
			break;
		case ACI_PCM_DEVICE:
			gDsmAudio.media_format = MDI_FORMAT_PCM_8K;
			break;
		case ACI_M4A_DEVICE:
			gDsmAudio.media_format = MDI_FORMAT_M4A;
			break;
		default:
			return MR_FAILED;
	}
#ifdef MMI_ON_HARDWARE_P
	kal_prompt_trace(MOD_MMI,"dsm_audio_load format = %d,media_type = %d",gDsmAudio.media_format,media_type);
#endif	
	gDsmAudio.state = MR_MEDIA_LOADED;
	
	if(is_file)
	{
		gDsmAudio.load_type = MR_MEDIA_FILE_LOAD;
		memset(gDsmAudio.filename,0,sizeof(gDsmAudio.filename));
		mr_fs_get_filename((char *)gDsmAudio.filename,media->buf);
		gDsmAudio.buflen  = mr_str_wstrlen((char *)gDsmAudio.filename);
		return MR_SUCCESS;
	}
	else
	{
		gDsmAudio.load_type = MR_MEDIA_BUF_LOAD;
		gDsmAudio.pbuf = media->buf;
		gDsmAudio.buflen = media->buf_len;
		return MR_SUCCESS;
	}
}


int32 mr_media_audio_load(int32 code, uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	switch(code /10)
	{
		case MR_MEDIA_BUF_LOAD:
		{
			return dsm_audio_load((mr_audio_load_info_t * )input, FALSE, code%10);
		}
		
		case MR_MEDIA_FILE_LOAD:
		{
			mr_audio_load_info_t load = {0};
			load.buf = (char *)input;
			load.buf_len = input_len;
			return dsm_audio_load(&load, TRUE, code%10);
		}
	}
}


static void dsm_play_audio(void)
{	
	gDsmAudio.state = MR_MEDIA_PLAYING;
	dsm_start_record_play_seconds();
       StartTimer( DSM_AUDIO_COUNT_TIMER, 500, dsm_audply_count_timer );
}


static void dsm_pause_audio(void)
{
	gDsmAudio.state = MR_MEDIA_PAUSED;
	dsm_update_play_seconds();
	StopTimer(DSM_AUDIO_COUNT_TIMER);
}


static void dsm_stop_audio(void)
{
	gDsmAudio.state = MR_MEDIA_LOADED;
	gDsmAudio.need_restore = FALSE;
	dsmLoopPCM = 0;
	StopTimer(DSM_AUDIO_COUNT_TIMER);
	dsm_clear_play_seconds();
}


static void dsm_resume_audio(void)
{	
	gDsmAudio.state = MR_MEDIA_PLAYING;
	dsm_start_record_play_seconds();
	StartTimer(DSM_AUDIO_COUNT_TIMER, 500, dsm_audply_count_timer );
}


static void dsm_play_callback( mdi_result result )
{	
	if(gDsmAudio.state != MR_MEDIA_PLAYING)
		return;
	
	switch( result )
	{
		case MDI_AUDIO_END_OF_FILE:
//#ifdef __MMI_DSM_NEW_JSKY__			
		    // 有些应用没有做上下文切换可能造成死机，如果有应用有问题的话，把传入的参数gDsmAudio.playCB设置为NULL
			if(gDsmAudio.playCB)
			{
				gDsmAudio.playCB(ACI_PLAY_COMPLETE);
				gDsmAudio.playCB = NULL;
			}
			else
			{
				mr_event(MR_AUDIO_EVENT, ACI_PLAY_COMPLETE, 0);
			}
			dsm_stop_audio();
//#endif			
			break;
		case MDI_AUDIO_TERMINATED:
			dsm_restore_const_seconds = gDsmAudio.const_seconds;
			dsm_restore_play_seconds = gDsmAudio.play_seconds;
			dsm_stop_audio();

			gDsmAudio.need_restore = FALSE;
			
			if(gDsmAudio.load_type == MR_MEDIA_FILE_LOAD)
			{
				if( mdi_audio_store_file(0) == MDI_AUDIO_SUCCESS)
				{
					gDsmAudio.need_restore = TRUE;
					gDsmAudio.state = MR_MEDIA_SUSPENDED;
				}
			}
			break;
		default:
			dsm_stop_audio();
//#ifdef __MMI_DSM_NEW_JSKY__			
		       //此处如果栈切换不到位,CB调用会造成当机.
			if(gDsmAudio.playCB)
			{
				gDsmAudio.playCB(ACI_PLAY_ERROR);
				gDsmAudio.playCB = NULL;
			}
			else
			{
				mr_event(MR_AUDIO_EVENT, ACI_PLAY_ERROR, 0);
			}
//#endif			
			
			break;
	}
}


static void dsm_pause_callback( mdi_result result )
{	
	switch( result )
	{
		case MDI_AUDIO_END_OF_FILE:
		case MDI_AUDIO_BAD_FORMAT:
		case MDI_AUDIO_TERMINATED:
			dsm_stop_audio();
			break;
		default:
			break;
	}
}


static void dsm_resume_callback( mdi_result result )
{	
	switch( result )
	{
		case MDI_AUDIO_END_OF_FILE:
		case MDI_AUDIO_BAD_FORMAT:
		case MDI_AUDIO_TERMINATED:
			dsm_stop_audio();
			break;
		default:
			break;
	}
}


int32 mr_media_audio_play_req(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	S32 result;
	mr_audio_play_info_t *playReq = NULL;
	
	if(input_len >= sizeof(mr_audio_play_info_t) && input!= NULL)
		playReq = (mr_audio_play_info_t *)input;
		
	if(gDsmAudio.state != MR_MEDIA_LOADED)
		return MR_FAILED;
	else
	{	
		gDsmAudio.play_type = DEVICE_AUDIO_PLAY_ONCE;
		gDsmAudio.playCB = NULL;
		gDsmAudio.isBlock = TRUE;
		dsmLoopPCM = 0;
		
		if(playReq)
		{
			gDsmAudio.playCB = playReq->cb;
			
			if(playReq->loop)
			{
				if(playReq->loop == 2)
					dsmLoopPCM = 1;
				
				gDsmAudio.play_type = DEVICE_AUDIO_PLAY_INFINITE;
			}
			
			if(!playReq->block)
				gDsmAudio.isBlock = FALSE;
		}

		if(gDsmAudio.load_type == MR_MEDIA_FILE_LOAD)
		{
			result = dsm_play_action_without_handle_result();
		}
		else 
		{
			dsmReadAddr = (int32)gDsmAudio.pbuf;
			
			if(gDsmAudio.isBlock)
			{
			#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
				result = mdi_audio_play_string_with_vol_path((void*)gDsmAudio.pbuf,(U32)gDsmAudio.buflen,gDsmAudio.media_format,
					                                                                     gDsmAudio.play_type,dsm_play_callback,NULL,MDI_AUD_VOL_EX_MUTE(gDsmAudio.volume),MDI_AUD_PTH_EX(MDI_DEVICE_SPEAKER2));			
			#else
				result = mdi_audio_play_string_with_vol_path((void*)gDsmAudio.pbuf,(U32)gDsmAudio.buflen,gDsmAudio.media_format,
					                                                                     gDsmAudio.play_type,NULL,dsm_play_callback,MDI_AUD_VOL_EX_MUTE(gDsmAudio.volume),MDI_AUD_PTH_EX(MDI_DEVICE_SPEAKER2));
			#endif		                                                                
			}
			else
			{
			#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
				result = mdi_audio_play_string_with_vol_path_non_block((void*)gDsmAudio.pbuf,(U32)gDsmAudio.buflen,gDsmAudio.media_format,
					                                                                     gDsmAudio.play_type,dsm_play_callback,NULL,MDI_AUD_VOL_EX_MUTE(gDsmAudio.volume),MDI_AUD_PTH_EX(MDI_DEVICE_SPEAKER2));			
			#else
				result = mdi_audio_play_string_with_vol_path_non_block((void*)gDsmAudio.pbuf,(U32)gDsmAudio.buflen,gDsmAudio.media_format,
					                                                                     gDsmAudio.play_type,NULL,dsm_play_callback,MDI_AUD_VOL_EX_MUTE(gDsmAudio.volume),MDI_AUD_PTH_EX(MDI_DEVICE_SPEAKER2));
			#endif		                                                                     
			}
		}
		
		if(result == MDI_AUDIO_SUCCESS)
		{
			dsm_play_audio();
			return MR_SUCCESS;
		}
		else
		{
			dsm_stop_audio();
			return MR_FAILED;
		}
		
	}
}


int32 mr_media_audio_pause_req(void)
{
	mdi_result result;
	
	if(gDsmAudio.state != MR_MEDIA_PLAYING)
		return MR_FAILED;

#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
	result = mdi_audio_pause(dsm_pause_callback, NULL);
#else
	result = mdi_audio_pause(gDsmAudio.mdi_h, dsm_pause_callback);
#endif	
	
	if( result == MDI_AUDIO_SUCCESS )
	{
		dsm_pause_audio();
		return MR_SUCCESS;
	}
	else
	{
		return MR_FAILED;
	}
}


int32 mr_media_audio_stop_req(void)
{
	mdi_result result;
	
	if(gDsmAudio.state == MR_MEDIA_PLAYING ||gDsmAudio.state == MR_MEDIA_PAUSED)
	{
		if(gDsmAudio.load_type == MR_MEDIA_FILE_LOAD)
			result = mdi_audio_stop_file();
		else
			result = mdi_audio_stop_string();
	
		if(result == MDI_AUDIO_SUCCESS)
		{
			dsm_stop_audio();
			return MR_SUCCESS;
		}
		else
		{
			return MR_FAILED;
		}
	}
	else
		return MR_FAILED;
}


int32 mr_media_audio_resume_req(void)
{
	mdi_result result;
	
	if(gDsmAudio.state != MR_MEDIA_PAUSED)
		return MR_FAILED;

#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
	result = mdi_audio_resume(dsm_resume_callback, NULL);
#else
	result = mdi_audio_resume(gDsmAudio.mdi_h, dsm_resume_callback);
#endif	
	
	if( result == MDI_AUDIO_SUCCESS )
	{
		dsm_resume_audio();
		return MR_SUCCESS;
	}
	else
	{
		return MR_FAILED;
	}
	
}


static void dsm_start_record_play_seconds(void)
{
	gDsmAudio.const_seconds += gDsmAudio.play_seconds;
	gDsmAudio.play_seconds = 0;
	kal_get_time( &gDsmAudio.play_time );
}


static S32 dsm_get_play_milliseconds(void)
{
	U32 progress;
	
	mdi_audio_get_progress_time(&progress);

	return (S32)progress;
}


int32 mr_media_audio_get_play_milliseconds(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	S32 time;
	time = dsm_get_play_milliseconds();
	
	if(time == -1)
	{
		return MR_FAILED;
	}
	else
	{
		g_mr_common_rsp.p1 = time;
		*output = (uint8 *)&g_mr_common_rsp;
		*output_len = sizeof(int32);
		return MR_SUCCESS;
	}
}


static S32 dsm_get_play_seconds(void)
{
	return (S32)((gDsmAudio.const_seconds + gDsmAudio.play_seconds) * 0.004615 + 0.5);
}


int32 mr_media_audio_get_play_seconds(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	S32 time;
	time = dsm_get_play_seconds();
	
	if(time == -1)
	{
		return MR_FAILED;
	}
	else
	{
		g_mr_common_rsp.p1 = time;
		*output = (uint8 *)&g_mr_common_rsp;
		*output_len = sizeof(int32);
		return MR_SUCCESS;
	}
}

#ifdef __MMI_DSM_NEW_JSKY__
int32 mr_media_audio_get_play_seconds_ms(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	S32 time;
	time = dsm_get_audio_time_ms();
		
	if(time == -1)
	{
		return MR_FAILED;
	}
	else
	{
		g_mr_common_rsp.p1 = time;
		*output = (uint8 *)&g_mr_common_rsp;
		*output_len = sizeof(int32);
		return MR_SUCCESS;
	}
}
#endif /*__MMI_DSM_NEW_JSKY__*/

int32 mr_media_close_device(void)
{
	mr_media_audio_stop_req();
	memset(&gDsmAudio,0,sizeof(gDsmAudio));
	gDsmAudio.state = MR_MEDIA_IDLE;
	return MR_SUCCESS;
}


static void dsm_update_play_seconds(void)
{
	kal_uint32 current_time;		
	kal_get_time( &current_time );
	gDsmAudio.play_seconds = current_time - gDsmAudio.play_time;
}


static void dsm_clear_play_seconds(void)
{
	gDsmAudio.const_seconds = 0;
	gDsmAudio.play_seconds = 0;
}


static void dsm_audply_count_timer(void)
{
	
	if( gDsmAudio.state == MR_MEDIA_PLAYING )
	{
		dsm_update_play_seconds();
		//StartTimer( DSM_AUDIO_COUNT_TIMER, 500, dsm_audply_count_timer );
		gui_start_timer(500,dsm_audply_count_timer);
	}
}

static S32 dsm_play_action_without_handle_result(void)
{
	mdi_result result;	
	
	dsm_clear_play_seconds();
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
	result = mdi_audio_play_file_with_vol_path( gDsmAudio.filename, gDsmAudio.play_type,NULL,
        		dsm_play_callback, NULL, MDI_AUD_VOL_EX_MUTE(gDsmAudio.volume), MDI_AUD_PTH_EX(MDI_DEVICE_SPEAKER2));
#else
	result = mdi_audio_play_file_with_vol_path( gDsmAudio.filename, gDsmAudio.play_type,NULL,
        		dsm_play_callback, MDI_AUD_VOL_EX_MUTE(gDsmAudio.volume), MDI_AUD_PTH_EX(MDI_DEVICE_SPEAKER2));
#endif


	return (S32) result;
}


void mr_media_audio_restore_play(void)
{
	S32 result = 0;
	
	if(gDsmAudio.need_restore == TRUE &&gDsmAudio.load_type == MR_MEDIA_FILE_LOAD)
	{
		gDsmAudio.need_restore = FALSE;
		gDsmAudio.const_seconds = dsm_restore_const_seconds;
		gDsmAudio.play_seconds = dsm_restore_play_seconds;

		result = dsm_restore_action_without_handle_result();

		if(result == MDI_AUDIO_SUCCESS)
		{
			dsm_play_audio();
		}
		else 
		{
			dsm_stop_audio();
		}
	}
}

static S32 dsm_restore_action_without_handle_result(void)
{
	mdi_result result;
#ifdef __MMI_AUDIO_TIME_STRETCH__
	S32 check_result;
#endif			
	{
		gDsmAudio.state = MR_MEDIA_LOADED; 
		
		/*Play Request*/
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
		result = mdi_audio_restore_file_with_vol_path( gDsmAudio.filename, gDsmAudio.play_type, NULL,
			dsm_play_callback, NULL, MDI_AUD_VOL_EX_MUTE(gDsmAudio.volume), MDI_AUD_PTH_EX(MDI_DEVICE_SPEAKER2));
#else
		result = mdi_audio_restore_file_with_vol_path( gDsmAudio.filename, gDsmAudio.play_type, NULL,
			dsm_play_callback, MDI_AUD_VOL_EX_MUTE(gDsmAudio.volume), MDI_AUD_PTH_EX(MDI_DEVICE_SPEAKER2));
#endif			

#ifdef __SLT_MMI_AUDIO_BUG49__ 
		if( result == MDI_AUDIO_SUCCESS||result == MDI_AUDIO_BAD_FORMAT)
#else
		if( result == MDI_AUDIO_SUCCESS )
#endif
		{
			gDsmAudio.state = MR_MEDIA_PLAYING;
		}
	}

	return (S32) result;
}

int32 mr_media_audio_set_progress_time(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	mdi_result result;
	mr_audio_position_info_t *pos = (mr_audio_position_info_t *)input;
	int32 ms = pos->pos*1000;
	
	if(gDsmAudio.load_type != MR_MEDIA_FILE_LOAD)
		return MR_FAILED;
	
	result = (mdi_result)media_aud_set_progress_time(MOD_MMI, ms);
	
	if(result == MDI_AUDIO_SUCCESS)
	{
		gDsmAudio.const_seconds = ms/4.615;
		return MR_SUCCESS;
	}
	else
		return MR_FAILED;
		
}


int32 mr_media_apm_alloc_buffer(void)
{
#ifndef WIN32
#if APM_SUPPORT //( defined(MT6225)||defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	APM_Allocate_Buffer(1024);
	dsm_is_apm_alloc = 1;
#endif
#endif
	return MR_SUCCESS;
}


int32 mr_media_apm_release_buffer(void)
{
#ifndef WIN32
#if APM_SUPPORT //( defined(MT6225)||defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	if(dsm_is_apm_alloc == 1)
	{
		APM_Release_Buffer();
		dsm_is_apm_alloc = 0;  
	}
#endif
#endif
	return MR_SUCCESS;
}


int32 mr_media_free_device(void)
{
	return MR_SUCCESS;
}


int32 mr_media_pcm_get_read_addr(int32 param)
{
	return dsmReadAddr;
}


int32 mr_media_pcm_set_write_addr(int32 param)
{
	dsmWriteAddr = param;
	return MR_SUCCESS;
}


int32 mr_media_mma_open(int32 code ,uint8 *input,int32 input_len)
{
	int ret ;
	U8 repeats = 1;
	
	mr_bg_play_info_t *req = (mr_bg_play_info_t*)input;

	if((req == NULL)
	||(input_len <sizeof(mr_bg_play_info_t))
	||(req->data == NULL)
	||(req->len==0)
	)
		return MR_FAILED;

	if(req->loop == 1)
		repeats = 0;
		
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
	switch(code%10)
	{
		case ACI_WAVE_DEVICE:
			ret = mdi_audio_mma_open_string(0, req->data, req->len, MDI_FORMAT_WAV, repeats, NULL, NULL);
			break;
		case ACI_MIDI_DEVICE:
			ret = mdi_audio_mma_open_string(0, req->data, req->len, MDI_FORMAT_SMF, repeats, NULL, NULL);
			break;
		default:
			return MR_FAILED;
	}
#else		
	switch(code%10)
	{
		case ACI_WAVE_DEVICE:
			ret = mdi_audio_mma_open_wav((void *)req->data, req->len, repeats,NULL);
			break;
		case ACI_MIDI_DEVICE:
			ret = mdi_audio_mma_open_midi((void *)req->data, req->len, repeats,NULL);
			break;
		default:
			return MR_FAILED;
	}
#endif
	
	if(ret == 0)
		return MR_FAILED;
	else
		return (ret+MR_PLAT_VALUE_BASE);
}


int32 mr_media_mma_play(int32 code ,uint8 *input,int32 input_len)
{				
	dsm_set_bgplay_vol();
	
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
	mdi_audio_mma_play(*((int32 *)input)-MR_PLAT_VALUE_BASE);
#else
	switch(code%10)
	{
		case ACI_WAVE_DEVICE:
		{
			mdi_audio_mma_play_wav(*((int32 *)input)-MR_PLAT_VALUE_BASE);
			break;
		}
		case ACI_MIDI_DEVICE:
		{
			mdi_audio_mma_play_midi(*((int32 *)input)-MR_PLAT_VALUE_BASE);
			break;
		}
		default:
			return MR_FAILED;
	}
#endif	
	return MR_SUCCESS;
}


int32 mr_media_mma_stop(int32 code ,uint8 *input,int32 input_len)
{
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
	mdi_audio_mma_stop(*((int32 *)input)-MR_PLAT_VALUE_BASE);
#else
	switch(code%10)
	{
		case ACI_WAVE_DEVICE:
			mdi_audio_mma_stop_wav(*((int32 *)input)-MR_PLAT_VALUE_BASE);
			break;
		case ACI_MIDI_DEVICE:
			mdi_audio_mma_stop_midi(*((int32 *)input)-MR_PLAT_VALUE_BASE);
			break;
		default:
			return MR_FAILED;
	}
#endif	
	return MR_SUCCESS;
}

int32 mr_media_mma_close(int32 code ,uint8 *input,int32 input_len)
{
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
	mdi_audio_mma_close(*((int32 *)input)-MR_PLAT_VALUE_BASE);
#else
	switch(code%10)
	{
		case ACI_WAVE_DEVICE:
			mdi_audio_mma_close_wav(*((int32 *)input)-MR_PLAT_VALUE_BASE);
			break;
		case ACI_MIDI_DEVICE:

			mdi_audio_mma_close_midi(*((int32 *)input)-MR_PLAT_VALUE_BASE);
			break;
		default:
			return MR_FAILED;
	}
#endif	
	return MR_SUCCESS;
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////
static void dsm_init_camera_tmp_info(void)
{
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	//changed by sky huangsunbo 20091125
	//return MR_IGNORE;
#else
	char asciiPath[32] ={0};
	/* __MMI_DSM_NEW_JSKY__ begin support camera */
	T_DSM_DISK_INFO *disk_info;
	int32 output_len;
	int32 ret;
	uint8 drv = 'c';
	ret = mr_fs_get_free_space(&drv, sizeof(drv), (uint8 **)&disk_info, &output_len, NULL);
	if (ret == MR_SUCCESS && disk_info->account*disk_info->unit >= 50*1024)
	{
		drv = MMI_CARD_DRV;
	}
	else
	{
		drv = MMI_PUBLIC_DRV;
	}
	sprintf(asciiPath,"%c:\\dsm_capture.jpg",drv);
	/* __MMI_DSM_NEW_JSKY__ end support camera */
	mmi_asc_to_ucs2((S8 *)gDsmCameraReq.tmpName, (S8 *)asciiPath);
	gDsmCameraReq.pTmp = NULL;
#endif
}

static void dsm_uninit_camera_tmp_info(void)
{
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) //huangsunbo 20100121 for 6223
	if(gDsmCameraReq.pTmp != NULL)
	{
		mmi_frm_scrmem_free(gDsmCameraReq.pTmp);
		gDsmCameraReq.pTmp =NULL;
	}
#endif
}

static void dsm_unInit_camera(void)
{
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 
	dsm_uninit_camera_tmp_info();
	FS_Delete((WCHAR *)gDsmCameraReq.tmpName);
	memset(&gDsmCameraReq,0,sizeof(gDsmCameraReq));
	gDsmCameraReq.state = DSM_CAMERA_UNINIT;
#endif
}

int32 mr_camera_init(int32 param)
{
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	return MR_IGNORE;
#else
	memset(&gDsmCameraReq,0,sizeof(gDsmCameraReq));
	gDsmCameraReq.state = DSM_CAMERA_INITED;
	gDsmCameraReq.last_error = MDI_RES_CAMERA_SUCCEED;
	//mmi_camera_load_setting();
	gDsmCameraReq.zoom_max = dsm_camera_get_zoom_total_level_cnt();
	return MR_SUCCESS;
#endif
}

static void dsm_resume_camera(void)
{
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
	gDsmCameraReq.last_error = mdi_camera_power_on(0);
#else
	gDsmCameraReq.last_error = mdi_camera_power_on();
#endif	
	dsm_entry_preview();
#endif
}

int32 mr_camera_get_zoom_max(int32 param)
{
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	return MR_IGNORE;
#else
	return gDsmCameraReq.zoom_max + MR_PLAT_VALUE_BASE;
#endif
}

int32 mr_camera_zoom_in(int32 param)
{
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	return MR_IGNORE;
#else

#ifdef MMI_ON_HARDWARE_P			
	kal_prompt_trace(MOD_MMI,"gDsmCameraReq.state = %d",gDsmCameraReq.state);
#endif			
	if(gDsmCameraReq.state == DSM_CAMERA_PREVIEW)
	{
		U16 zoom_factor;
			
		if((gDsmCameraReq.zoom_index) < (gDsmCameraReq.zoom_max-1))
		{			
			gDsmCameraReq.zoom_index += 1;

			zoom_factor = dsm_camera_preview_get_zoom_factor();	

			mdi_camera_update_para_zoom(zoom_factor);
		}
		return MR_SUCCESS;
	}
	return MR_FAILED;
#endif
}

int32 mr_camera_zoom_out(int32 param)
{
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	return MR_IGNORE;
#else
#ifdef MMI_ON_HARDWARE_P			
	kal_prompt_trace(MOD_MMI,"gDsmCameraReq.state = %d",gDsmCameraReq.state);
#endif

	if(gDsmCameraReq.state == DSM_CAMERA_PREVIEW)
	{
		U16 zoom_factor;
		
		if(gDsmCameraReq.zoom_index >0)
		{
			gDsmCameraReq.zoom_index -= 1;
			zoom_factor = dsm_camera_preview_get_zoom_factor();	
		
			mdi_camera_update_para_zoom(zoom_factor);
		}
		return MR_SUCCESS;
	}
	return MR_FAILED;
#endif
}

int32 mr_camera_resume_ex(int32 param)
{			
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	return MR_IGNORE;
#else
	if(gDsmCameraReq.state == DSM_CAMERA_PAUSED)
	{
		/* __MMI_DSM_NEW_JSKY__ begin support camera */
		if (!gDsmCameraReq.is_from_jsky)
		{
			gdi_layer_push_and_set_active(gDsmCameraReq.osd_layer);
			gdi_layer_set_source_key(TRUE, GDI_COLOR_TRANSPARENT);	
			gdi_layer_pop_and_restore_active();	
		}
		/* __MMI_DSM_NEW_JSKY__ end support camera */
		dsm_resume_camera();
		return MR_SUCCESS;
	}
	else if(gDsmCameraReq.state == DSM_CAMERA_CAPTURED)
	{
		/* __MMI_DSM_NEW_JSKY__ begin support camera */
		if (!gDsmCameraReq.is_from_jsky)
		{
			gdi_layer_push_and_set_active(gDsmCameraReq.osd_layer);
			gdi_layer_set_source_key(TRUE, GDI_COLOR_TRANSPARENT);	
#if defined(__CAMERA_OSD_HORIZONTAL__)
			gdi_draw_solid_rect(gDsmCameraReq.req_x,gDsmCameraReq.req_y,gDsmCameraReq.req_w+gDsmCameraReq.req_x-1,gDsmCameraReq.req_h+gDsmCameraReq.req_y-1,GDI_COLOR_TRANSPARENT);
#else		
			gdi_draw_solid_rect(gDsmCameraReq.pre_req.preview_x,gDsmCameraReq.pre_req.preview_y,gDsmCameraReq.pre_req.preview_w+gDsmCameraReq.pre_req.preview_x-1,gDsmCameraReq.pre_req.preview_h+gDsmCameraReq.pre_req.preview_y-1,GDI_COLOR_TRANSPARENT);
#endif
			gdi_layer_pop_and_restore_active();	
		}
		/* __MMI_DSM_NEW_JSKY__ end support camera */
		dsm_entry_preview();
		return MR_SUCCESS;
	}
	return MR_FAILED;
#endif
}

// SKY_MULTIMEDIA 修改获取camera数据格式不对导致绿屏的问题
#if defined(MT6252) || defined(MT6255)  || defined(MT6250) 
#include "med_utility.h"
unsigned char *gDsmCamBuffer = NULL;
#endif
static int32 dsm_get_preview_data(mr_layer_info_t *layer)
{
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 

	if(gDsmCameraReq.state == DSM_CAMERA_PREVIEW&&layer != NULL)
	{
		gdi_layer_push_and_set_active(gDsmCameraReq.preview_layer);
		layer->handle = (int32)gdi_act_layer;
		layer->w = gdi_act_layer->width;
		layer->h = gdi_act_layer->height;
		layer->buffer = (char*)gdi_act_layer->buf_ptr;

	// SKY_MULTIMEDIA 修改获取camera数据格式不对导致绿屏的问题
	#if defined(MT6252) || defined(MT6255)  || defined(MT6250) 
		if (GDI_COLOR_FORMAT_UYVY422 == gdi_act_layer->cf)//rwei
		{
			if (!gDsmCamBuffer)
			{
				gDsmCamBuffer = (unsigned char *)med_alloc_ext_mem(gdi_act_layer->width*gdi_act_layer->height*2);
				kal_prompt_trace(MOD_MMI,"[YUZF] malloc gDsmCamBuffer %x", gDsmCamBuffer);
			}
			if (gDsmCamBuffer)
			{
				iul_csc_uyvy422_to_rgb565_fxp_dithering(
				    gdi_act_layer->buf_ptr, gDsmCamBuffer,  gdi_act_layer->width, gdi_act_layer->height);
				layer->buffer = gDsmCamBuffer;
			}
			else
			{
				iul_csc_uyvy422_to_rgb565_fxp_dithering(
				    gdi_act_layer->buf_ptr, gdi_act_layer->buf_ptr,  gdi_act_layer->width, gdi_act_layer->height);
			}
		 
		}
	#endif
		gdi_layer_pop_and_restore_active();
		return MR_SUCCESS;
	}
	else
	{
		return MR_FAILED;
	}
#endif
}

static int32 dsm_create_camera(mr_camera_launch_info_t *pReq,int32 *handler,int32* osd_buffer)
{	
	if(handler == NULL||osd_buffer == NULL)
		return MR_FAILED;
	
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
		return MR_IGNORE;
#else

	/* __MMI_DSM_NEW_JSKY__ begin support camera */
	if(gDsmCameraReq.state != DSM_CAMERA_INITED)
		return MR_FAILED;

	if ((pReq->img_qt & 0x0000ffff) > DSM_CAMEAR_QT_HIGHT)
	{
		return MR_FAILED;
	}
	
	gDsmCameraReq.is_from_jsky = (int32)((pReq->img_qt & 0xffff0000) != 0);

	memcpy(&gDsmCameraReq,pReq,sizeof(mr_camera_launch_info_t));

	gDsmCameraReq.pre_req.img_qt = (uint32)(gDsmCameraReq.pre_req.img_qt & 0x0000ffff);
	/* __MMI_DSM_NEW_JSKY__ end support camera */

#if defined(__CAMERA_OSD_HORIZONTAL__) 
	gDsmCameraReq.req_x = gDsmCameraReq.pre_req.preview_x;
	gDsmCameraReq.req_y = gDsmCameraReq.pre_req.preview_y;
	gDsmCameraReq.req_w = gDsmCameraReq.pre_req.preview_w;
	gDsmCameraReq.req_h = gDsmCameraReq.pre_req.preview_h;
	gDsmCameraReq.pre_req.preview_x =0;
	gDsmCameraReq.pre_req.preview_y =0;
	gDsmCameraReq.pre_req.preview_w = LCD_HEIGHT;
	gDsmCameraReq.pre_req.preview_h = LCD_WIDTH;

	if(gDsmCameraReq.req_x !=0||gDsmCameraReq.req_y !=0)
	{
		gDsmCameraReq.pre_req.img_w = LCD_WIDTH;
		gDsmCameraReq.pre_req.img_h = LCD_HEIGHT;
	}
#endif	
	//gDsmCameraReq.pre_req.img_qt = gDsmCameraReq.pre_req.img_qt>(CAMERA_SETTING_IMAGE_QTY_COUNT-1)?(CAMERA_SETTING_IMAGE_QTY_COUNT-1):gDsmCameraReq.pre_req.img_qt;
	gDsmCameraReq.pre_req.img_qt = gDsmCameraReq.pre_req.img_qt>(MDI_CAMERA_JPG_QTY_COUNT-1)?(MDI_CAMERA_JPG_QTY_COUNT-1):gDsmCameraReq.pre_req.img_qt;
	dsmEntryCamera();

	*handler= gDsmCameraReq.camera_handler;
	*osd_buffer = (int32)gDsmCameraReq.osd_layer_buffer;
	return MR_SUCCESS;
#endif
}

static int32 dsm_capture(uint8 flag,int32 *ptr,int32 *size)
{
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	return MR_IGNORE;
#else
	extern MDI_RESULT	mdi_camera_capture_to_memory_dsm(U8 **buf_pp, U32 *captured_size,kal_uint8 continue_capture);
	extern MDI_RESULT mdi_camera_capture_to_file(S8 *file_name, U8 continue_capture);
       extern MDI_RESULT mdi_camera_capture_layer_to_file(U32 capture_layer_flag,S8 *file_name,S32 offset_x,S32 offset_y,S32 width, S32 height,U8 continue_capture);
	int fh = 0;
	UINT fSize,read;

	if(size == NULL||ptr == NULL)
		return MR_FAILED;
	
	if(gDsmCameraReq.state == DSM_CAMERA_PREVIEW)
	{
		extern int isDsmUseCamera;
		gDsmCameraReq.continue_capture = flag;				
		isDsmUseCamera =1;
		
#if 1	// #if defined(__CAMERA_OSD_HORIZONTAL__) 
		kal_prompt_trace(MOD_MMI,"gDsmCameraReq.pre_req.img_w=%d",gDsmCameraReq.pre_req.img_w);
		kal_prompt_trace(MOD_MMI,"gDsmCameraReq.pre_req.img_h=%d",gDsmCameraReq.pre_req.img_h);
		/* __MMI_DSM_NEW_JSKY__ begin support camera */
		gDsmCameraReq.last_error = mdi_camera_capture_layer_to_file(gDsmCameraReq.is_from_jsky ? GDI_LAYER_ENABLE_LAYER_1 : GDI_LAYER_ENABLE_LAYER_0,(S8 *)gDsmCameraReq.tmpName,gDsmCameraReq.pre_req.preview_x,gDsmCameraReq.pre_req.preview_y,gDsmCameraReq.pre_req.img_w,gDsmCameraReq.pre_req.img_h,gDsmCameraReq.continue_capture);
		/* __MMI_DSM_NEW_JSKY__ end support camera */
#else		
		gDsmCameraReq.last_error = mdi_camera_capture_to_file((S8 *)gDsmCameraReq.tmpName,gDsmCameraReq.continue_capture);
#endif
		dsm_uninit_camera_tmp_info();
		
		isDsmUseCamera =0;

#ifdef MMI_ON_HARDWARE_P				
		kal_prompt_trace(MOD_MMI,"---capture--- =%d",gDsmCameraReq.last_error);
#endif

		if(gDsmCameraReq.last_error == MDI_RES_CAMERA_SUCCEED)
		{

#if !(MDI_CAMERA_MT6238_SERIES) 	
			mdi_camera_save_captured_image();
#endif

			gDsmCameraReq.state = DSM_CAMERA_CAPTURED;

			fh=FS_Open((WCHAR *)gDsmCameraReq.tmpName,FS_READ_ONLY);
			
			if(fh >= FS_NO_ERROR)
			{
				FS_GetFileSize(fh,&fSize);
				gDsmCameraReq.pTmp = mmi_frm_scrmem_alloc(fSize);
				if(gDsmCameraReq.pTmp != NULL)
				{
					FS_Read(fh,(void *)gDsmCameraReq.pTmp,fSize,&read);
					FS_Close(fh);
					
					*ptr = (int32)gDsmCameraReq.pImg= (int32)gDsmCameraReq.pTmp;
					*size = gDsmCameraReq.ImgSize=fSize;

					/* __MMI_DSM_NEW_JSKY__ begin support camera */
					if(gDsmCameraReq.continue_capture == 1)
					/* __MMI_DSM_NEW_JSKY__ end support camera */
					{
						dsm_camera_preview_start();
						if(gDsmCameraReq.last_error == MDI_RES_CAMERA_SUCCEED)
							gDsmCameraReq.state = DSM_CAMERA_PREVIEW;
							gDsmCameraReq.continue_capture = 0;
					}
					return MR_SUCCESS;	
				}
				FS_Close(fh);
			}
		}
	}
#ifdef MMI_ON_HARDWARE_P			
	kal_prompt_trace(MOD_MMI,"capture param err");
#endif
	return MR_FAILED;
#endif
}

int32 mr_camera_pause(int32 param)
{
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	return MR_IGNORE;
#else
	if(gDsmCameraReq.state == DSM_CAMERA_PREVIEW)
	{
		mdi_camera_preview_stop();
		mdi_camera_power_off();
		dsm_camera_preview_set_blt_layer(FALSE);
		/* __MMI_DSM_NEW_JSKY__ begin support camera */
		if (!gDsmCameraReq.is_from_jsky)
		{
			gdi_layer_push_and_set_active(gDsmCameraReq.osd_layer);
			gdi_layer_clear_background(GDI_COLOR_TRANSPARENT);
			gdi_layer_set_source_key(FALSE, GDI_COLOR_TRANSPARENT);	
			gdi_layer_pop_and_restore_active();	
		}
		/* __MMI_DSM_NEW_JSKY__ end support camera */
		gDsmCameraReq.state = DSM_CAMERA_PAUSED;
		return MR_SUCCESS;
	}
	else
	{
		return MR_FAILED;
	}
#endif	
}

static void dsm_camera_free_resource(void)
{
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 
	/* __MMI_DSM_NEW_JSKY__ begin support camera */
	gdi_handle baseHandler;
	 
	mr_trace("dsm_camera_free_resource, gDsmCameraReq.osd_layer = 0X%08x", gDsmCameraReq.osd_layer);

	gDsmCameraReq.osd_layer = GDI_LAYER_EMPTY_HANDLE;
	gDsmCameraReq.osd_layer_buffer = NULL;

	gdi_layer_restore_base_active();
	/* __MMI_DSM_NEW_JSKY__ end support camera */
	// SKY_MULTIMEDIA 修改获取camera数据格式不对导致绿屏的问题
#if defined(MT6252) || defined(MT6255)  || defined(MT6250) 
	if (gDsmCamBuffer)//rwei
	{
		med_free_ext_mem((void **)&gDsmCamBuffer);
		gDsmCamBuffer = NULL;
	}
#endif
#endif
}

static void dsm_camera_free_preview_resource(void)
{
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 
	/* __MMI_DSM_NEW_JSKY__ begin support camera */
	mr_trace("dsm_camera_free_preview_resource gDsmCameraReq.preview_layer = 0X%08x", gDsmCameraReq.preview_layer);

	if (gDsmCameraReq.preview_layer != GDI_LAYER_EMPTY_HANDLE)
	{
		gdi_layer_free(gDsmCameraReq.preview_layer);
		gDsmCameraReq.preview_layer = GDI_LAYER_EMPTY_HANDLE;
		mmi_frm_scrmem_free((void*)gDsmCameraReq.preview_layer_buffer);
		gDsmCameraReq.preview_layer_buffer = NULL;
	}
	/* __MMI_DSM_NEW_JSKY__ end support camera */
#endif
}

static void dsm_camera_create_preview_resource(void)
{
/* __MMI_DSM_NEW_JSKY__ begin support camera */
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 
	int32 buf_size;

	buf_size = ((gDsmCameraReq.pre_req.preview_w*gDsmCameraReq.pre_req.preview_h*GDI_MAINLCD_BIT_PER_PIXEL)>>3);
	gDsmCameraReq.preview_layer_buffer = mmi_frm_scrmem_alloc_framebuffer(buf_size);

	mr_trace("dsm_camera_create_preview_resource, pre_x = %d, pre_y = %d, pre_w = %d, pre_h = %d, buf_size = %d, ", 
	gDsmCameraReq.pre_req.preview_x, gDsmCameraReq.pre_req.preview_y, gDsmCameraReq.pre_req.preview_w, gDsmCameraReq.pre_req.preview_h, buf_size);

#if defined(__MMI_SCREEN_ROTATE__)
	//__MMI_DSM_NEW_JSKY__
	if (current_screen_rotation != MMI_FRM_SCREEN_ROTATE_0)
	{
		if (current_screen_rotation == MMI_FRM_SCREEN_ROTATE_90 ||
			current_screen_rotation == MMI_FRM_SCREEN_ROTATE_270 ||
			current_screen_rotation == MMI_FRM_SCREEN_ROTATE_MIRROR_90 ||
			current_screen_rotation == MMI_FRM_SCREEN_ROTATE_MIRROR_270)
		{
			uint32 tmp = gDsmCameraReq.pre_req.preview_h;
			gDsmCameraReq.pre_req.preview_h = gDsmCameraReq.pre_req.preview_w;
			gDsmCameraReq.pre_req.preview_w = tmp;
			if (current_screen_rotation == MMI_FRM_SCREEN_ROTATE_270)
			{
				tmp = gDsmCameraReq.pre_req.preview_x;
				gDsmCameraReq.pre_req.preview_x = LCD_WIDTH - gDsmCameraReq.pre_req.preview_w - gDsmCameraReq.pre_req.preview_y;
				gDsmCameraReq.pre_req.preview_y = tmp;
			}
			else
			{
				tmp = gDsmCameraReq.pre_req.preview_y;
				gDsmCameraReq.pre_req.preview_y = LCD_HEIGHT - gDsmCameraReq.pre_req.preview_h - gDsmCameraReq.pre_req.preview_x;
				gDsmCameraReq.pre_req.preview_x = tmp;
			}
		}
	}
#endif

	gdi_layer_create_using_outside_memory(
		gDsmCameraReq.pre_req.preview_x,
		gDsmCameraReq.pre_req.preview_y,
		gDsmCameraReq.pre_req.preview_w,
		gDsmCameraReq.pre_req.preview_h,
		&gDsmCameraReq.preview_layer,
		(PU8)gDsmCameraReq.preview_layer_buffer, 
		buf_size);

	gdi_layer_push_and_set_active(gDsmCameraReq.preview_layer);
	gdi_layer_clear(GDI_COLOR_BLACK);
#if defined(__MMI_SCREEN_ROTATE__)
	//__MMI_DSM_NEW_JSKY__
	if (current_screen_rotation != MMI_FRM_SCREEN_ROTATE_0)
	{
		gdi_layer_set_rotate(current_screen_rotation);
		//gdi_layer_set_source_key(TRUE, GDI_COLOR_TRANSPARENT);
		//gdi_layer_clear(GDI_COLOR_TRANSPARENT);
	}
#endif
	gdi_layer_pop_and_restore_active();
#endif
/* __MMI_DSM_NEW_JSKY__ end support camera */
}

static BOOL dsm_camera_create_resource(void)
{
	/*----------------------------------------------------------------*/
	/* Local Variables                                                */
	/*----------------------------------------------------------------*/
/* __MMI_DSM_NEW_JSKY__ begin support camera */
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 

	gdi_layer_get_base_handle(&gDsmCameraReq.osd_layer);
	gdi_layer_push_and_set_active(gDsmCameraReq.osd_layer);
	gdi_layer_get_buffer_ptr((U8 **)&gDsmCameraReq.osd_layer_buffer);
	if (!gDsmCameraReq.is_from_jsky)
	{
		gdi_layer_set_source_key(TRUE, GDI_COLOR_TRANSPARENT);
		gdi_draw_solid_rect(
			gDsmCameraReq.pre_req.preview_x,
			gDsmCameraReq.pre_req.preview_y,
			gDsmCameraReq.pre_req.preview_x + gDsmCameraReq.pre_req.preview_w - 1,
			gDsmCameraReq.pre_req.preview_y + gDsmCameraReq.pre_req.preview_h - 1,
			GDI_COLOR_TRANSPARENT);
	}
	gdi_layer_pop_and_restore_active();
#endif
/* __MMI_DSM_NEW_JSKY__ begin support camera */
	return TRUE;
}

static U16 dsm_camera_preview_get_zoom_factor(void)
{
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 

   /*----------------------------------------------------------------*/
   /* Code Body                                                      */
   /*----------------------------------------------------------------*/
#if 1  
	return gDsmCameraReq.zoom_index + 10;
#else
       extern int g_camera_feature_zoom[];
	return g_camera_feature_zoom[gDsmCameraReq.zoom_index];
#endif	
#endif
}

static void dsm_camera_preview_set_blt_layer(BOOL is_previewing)
{
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 
/* __MMI_DSM_NEW_JSKY__ begin support camera */
	/*----------------------------------------------------------------*/
	/* Code Body                                                      */
	/*----------------------------------------------------------------*/
#ifdef MMI_ON_HARDWARE_P
	kal_prompt_trace(MOD_MMI,"dsm_camera_preview_set_blt_layer");
	kal_prompt_trace(MOD_MMI,"gDsmCameraReq.preview_layer = %d",gDsmCameraReq.preview_layer);
	kal_prompt_trace(MOD_MMI,"gDsmCameraReq.base_layer = %d",gDsmCameraReq.base_layer);
	kal_prompt_trace(MOD_MMI,"gDsmCameraReq.osd_layer = %d",gDsmCameraReq.osd_layer);
#endif

	if (is_previewing)
	{
		if (!gDsmCameraReq.is_from_jsky)
		{
			gdi_layer_set_blt_layer(gDsmCameraReq.preview_layer, gDsmCameraReq.osd_layer, 0, 0);
		}
		else
		{
			gdi_layer_set_blt_layer(gDsmCameraReq.osd_layer, gDsmCameraReq.preview_layer, 0, 0);
		}
	}
	else
	{
		gdi_layer_set_blt_layer(gDsmCameraReq.base_layer, 0, 0, 0);
	}
/* __MMI_DSM_NEW_JSKY__ begin support camera */
#endif
}

static void dsm_camera_preview_start(void)
{
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 

	extern U8 camera_image_qty_command_map[];
	mdi_camera_setting_struct camera_setting_data = {0};
	mdi_camera_preview_struct	preview_setting_data = {0};

	mdi_camera_load_default_setting(&camera_setting_data);

	camera_setting_data.zoom = dsm_camera_preview_get_zoom_factor();
	//camera_setting_data.image_qty			= camera_image_qty_command_map[gDsmCameraReq.pre_req.img_qt];
	camera_setting_data.image_qty			= gDsmCameraReq.pre_req.img_qt;
	camera_setting_data.image_width		= gDsmCameraReq.pre_req.img_w;
	camera_setting_data.image_height		= gDsmCameraReq.pre_req.img_h;	
	camera_setting_data.lcm					= MDI_CAMERA_PREVIEW_LCM_MAINLCD;
#if !(__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)	
	camera_setting_data.lcm_rotate = MDI_CAMERA_LCM_ROTATE_0;
	camera_setting_data.preview_rotate = MDI_CAMERA_PREVIEW_ROTATE_0;
	camera_setting_data.continue_capture = gDsmCameraReq.continue_capture;	
#endif	

#if defined(HORIZONTAL_CAMERA) && defined(__MDI_CAMERA_HW_ROTATOR_BY_IDP__)
/* __MMI_DSM_NEW_JSKY__ begin support screen rotate */
#if defined(__MMI_SCREEN_ROTATE__)
	if (current_screen_rotation == MMI_FRM_SCREEN_ROTATE_270)
	{
		camera_setting_data.preview_width = gDsmCameraReq.pre_req.preview_w;;
		camera_setting_data.preview_height = gDsmCameraReq.pre_req.preview_h;	 
	}
	else
	{
#endif
	    camera_setting_data.preview_width = gDsmCameraReq.pre_req.preview_h;;
	    camera_setting_data.preview_height = gDsmCameraReq.pre_req.preview_w;    
#if defined(__MMI_SCREEN_ROTATE__)
	}
#endif
/* __MMI_DSM_NEW_JSKY__ end support screen rotate */
#else /*  defined(HORIZONTAL_CAMERA) && defined(__MDI_CAMERA_HW_ROTATOR_BY_IDP__) */
    camera_setting_data.preview_width = gDsmCameraReq.pre_req.preview_w;
    camera_setting_data.preview_height = gDsmCameraReq.pre_req.preview_h;;
#endif /*  defined(HORIZONTAL_CAMERA) && defined(__MDI_CAMERA_HW_ROTATOR_BY_IDP__) */

#if defined(HORIZONTAL_CAMERA) && defined(__MDI_CAMERA_HW_ROTATOR_SUPPORT__)
/* __MMI_DSM_NEW_JSKY__ begin support screen rotate */
#if defined(__MMI_SCREEN_ROTATE__)
	if (current_screen_rotation != MMI_FRM_SCREEN_ROTATE_270)
#endif
/* __MMI_DSM_NEW_JSKY__ end support screen rotate */
	mdi_camera_set_hw_rotator(MDI_CAMERA_PREVIEW_ROTATE_90);
#endif /*  defined(HORIZONTAL_CAMERA) && defined(__MDI_CAMERA_HW_ROTATOR_SUPPORT__) */

	preview_setting_data.blt_layer_flag = GDI_LAYER_ENABLE_LAYER_0|GDI_LAYER_ENABLE_LAYER_1;;
	preview_setting_data.is_lcd_update = TRUE;
	preview_setting_data.is_tvout = FALSE;
	/* __MMI_DSM_NEW_JSKY__ begin support camera */
	preview_setting_data.preview_layer_flag = gDsmCameraReq.is_from_jsky ? GDI_LAYER_ENABLE_LAYER_1 : GDI_LAYER_ENABLE_LAYER_0;
	/* __MMI_DSM_NEW_JSKY__ end support camera */
	preview_setting_data.preview_layer_handle = gDsmCameraReq.preview_layer;
#if !(__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)	
	preview_setting_data.preview_wnd_offset_x = gDsmCameraReq.pre_req.preview_x;
	preview_setting_data.preview_wnd_offset_y = gDsmCameraReq.pre_req.preview_y;
	preview_setting_data.preview_wnd_width = gDsmCameraReq.pre_req.preview_w;
	preview_setting_data.preview_wnd_height = gDsmCameraReq.pre_req.preview_h;
#endif	
	preview_setting_data.src_key_color = GDI_COLOR_TRANSPARENT;	

#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
	gDsmCameraReq.last_error = mdi_camera_preview_start(&preview_setting_data, &camera_setting_data, NULL);
#else
	gDsmCameraReq.last_error = mdi_camera_preview_start(&preview_setting_data, &camera_setting_data);
#endif	
	
#ifdef MMI_ON_HARDWARE_P		   
	kal_prompt_trace(MOD_MMI,"gDsmCameraReq.last_error =%d",gDsmCameraReq.last_error);
#endif

#endif
}

static void dsm_entry_preview(void)
{
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 
	/* __MMI_DSM_NEW_JSKY__ begin support camera */
	if (!gDsmCameraReq.is_from_jsky)
	{
		gdi_layer_push_and_set_active(gDsmCameraReq.base_layer);
		//gdi_layer_clear_background(GDI_COLOR_BLACK);
		gdi_layer_pop_and_restore_active();	
	}
	/* __MMI_DSM_NEW_JSKY__ end support camera */
	dsm_camera_preview_set_blt_layer(FALSE);
	gdi_layer_blt_previous(0, 0, UI_device_width-1,UI_device_height-1);
	dsm_camera_free_preview_resource();
	dsm_camera_create_preview_resource();
	dsm_camera_preview_set_blt_layer(TRUE);
	/* __MMI_DSM_NEW_JSKY__ begin support camera */
	mmi_frm_set_curr_scr_blt_mode(MMI_FRM_SCR_BLT_IMMEDIATE);	
	gdi_layer_blt_previous(0, 0, LCD_WIDTH - 1, LCD_HEIGHT - 1);
	/* __MMI_DSM_NEW_JSKY__ end support camera */
	dsm_camera_preview_start();
	gDsmCameraReq.state = DSM_CAMERA_PREVIEW;
#endif
}

static MDI_RESULT	mdi_camera_capture_to_memory_dsm(U8 **buf_pp, U32 *captured_size,kal_uint8 continue_capture)
{
#if 0
   /*----------------------------------------------------------------*/
   /* Local Variables                                                */
   /*----------------------------------------------------------------*/
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	return MR_IGNORE;
#else
	media_cam_capture_req_struct capture_data ={0};
	S32 result;
	extern mdi_camera_context_struct				*mdi_camera_p;
	extern mdi_camera_setting_struct				*camera_setting_p;
	extern mdi_camera_context_struct				g_mdi_camera_cntx;
   /*----------------------------------------------------------------*/
   /* Code Body                                                      */
   /*----------------------------------------------------------------*/
	if(mdi_camera_p->state != 2)
		ASSERT(0);

#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) //huangsunbo 20100517
#if (MTK_VERSION >= 0x0852)
{
	char asciiPath[32] ={0};
	sprintf(asciiPath,"%c:\\dsm_capture.jpg",MMI_CARD_DRV);
	mmi_asc_to_ucs2((S8 *)gDsmCameraReq.tmpName, (S8 *)asciiPath);
}
#endif

#endif

	capture_data.image_width 	= camera_setting_p->image_width;
	capture_data.image_height 	= camera_setting_p->image_height;

	capture_data.media_mode			= MED_MODE_BUFFER;
	capture_data.image_quality		= (U8)camera_setting_p->image_qty;
	capture_data.source_device		= CAM_SRC_ISP;		/* ISP */
	capture_data.capture_buffer_p = (void**)buf_pp;
	
#if  ( (MTK_VERSION >= 0x0852) && (!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) //huangsunbo 20100517
	capture_data.data = gDsmCameraReq.tmpName;
#else
	capture_data.data					= NULL;				/* not used */
#endif
	capture_data.file_buffer_len	= 0;					/* not used */
	capture_data.file_size_p		= (kal_uint32*)captured_size;
	capture_data.flash_mode			= (kal_uint16)camera_setting_p->flash;	
	capture_data.snapshot_number 	= 1;
	capture_data.gray_mode			= 0;
	capture_data.continue_capture = continue_capture;
		
	#if (defined(MT6228)||defined(MT6229))
		capture_data.thumbnail_mode		= FALSE;
	
    #ifdef __MTK_TARGET__
    capture_data.image_data_format = IMGDMA_IBW_OUTPUT_RGB565;
    #endif

		capture_data.image_pitch_mode		= FALSE;
		capture_data.tv_output				= FALSE;		
		capture_data.manual_wb				= FALSE;
		capture_data.display					= FALSE;

		capture_data.overlay_frame_mode				= camera_setting_p->overlay_frame_mode;
		capture_data.overlay_color_depth			= camera_setting_p->overlay_frame_depth;
		capture_data.overlay_frame_source_key			= camera_setting_p->overlay_frame_source_key;
		capture_data.overlay_frame_width			= camera_setting_p->overlay_frame_width;
		capture_data.overlay_frame_height			= camera_setting_p->overlay_frame_height;
		capture_data.overlay_frame_buffer_address		= camera_setting_p->overlay_frame_buffer_address;   
	#endif /* MT6228, MT6229 */
	


#ifdef __MMI_TVOUT__
	/* temp solution for 200M pixtel catpure */
	/* the bandwidth is no enoguth for 200M capture with TV-out enable */
	if(g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
	{
     mdi_tvout_sleep_in();
   }
#endif

	/* capture jpeg */
	#ifdef __MTK_TARGET__
	#if (MTK_VERSION >= 0x06b)
	    result = media_cam_capture(stack_int_get_active_module_id(), &capture_data);
	#else
		result = media_cam_capture(MOD_MMI, &capture_data);
	#endif
	#else
		/* avoid simulaotr compile warning */
		result = MED_RES_OK;
	#endif /* __MTK_TARGET__ */
	
#ifdef __MMI_TVOUT__
	/* temp solution for 200M pixtel catpure */
	/* the bandwidth is no enoguth for 200M capture with TV-out enable */
	if(g_mdi_camera_cntx.is_tvout && mdi_tvout_is_enable())
	{
     mdi_tvout_sleep_out();
   }
#endif

	/* capture will also stop preview */
	mdi_camera_p->is_preview_stopped = TRUE;

	/* after captured. back to idle state */
	mdi_camera_p->state = 1;

	/* capture will also stop preview TV-Out */
	#ifdef __MMI_TVOUT__
		/* restore owner to GDI */
		if(g_mdi_camera_cntx.is_tvout)
		{
			if(mdi_tvout_is_enable())
			{
				if(g_mdi_camera_cntx.tvout_prev_owner == MDI_TV_OWNER_GDI)
				{
					mdi_tvout_set_owner(MDI_TV_OWNER_GDI);	
				}
			}
		}
	#endif /* __MMI_TVOUT__ */

	if(result == MED_RES_OK)
		return MDI_RES_CAMERA_SUCCEED;
	else if(result == MED_RES_DISC_FULL)
		return MDI_RES_CAMERA_ERR_DISK_FULL;
	else if(result == MED_RES_WRITE_PROTECTION)
		return MDI_RES_CAMERA_ERR_WRITE_PROTECTION;
	else if(result == MED_RES_NO_DISC)
		return MDI_RES_CAMERA_ERR_NO_DISK;
	else
		return MDI_RES_CAMERA_ERR_FAILED;
    #endif
#endif    
}


static S32 dsm_camera_get_zoom_total_level_cnt(void)
{
	S32 i = 0;
#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) //huangsunbo 20100121 for 6223
#if 1
	i = mdi_camera_get_max_zoom_factor(LCD_WIDTH, LCD_HEIGHT);

	if (i < 10) i = 10;

	i = i - 10 + 1;
#else
	for (i= 0;i<CAMERA_FEATURE_ZOOM_COUNT;i++)
	{
		if(g_camera_feature_zoom[i] == g_camera_cntx.zoom_limit)
			break;
	}
#endif
#endif
	return i;
}


///////////////////////////////////////////////////////////////////////////////////////////
#include "mmiapi_enum.h"

#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00))) 
mr_camera_ctrl_t gDsmCameraReq ={0};
#endif


#if ((!defined(MT6223P))&&(!defined(MT6223))&&(!defined(MT6223P_S00)))

static void dsmExitCamera(void)
{
	gdi_handle baseHandle;
	extern pBOOL IsBackHistory;	 
	
/*保证不会出现重入mythroad的情况********/
	if(gDsmCameraReq.state != DSM_CAMERA_UNINIT)
		mr_event(MR_OSD_EVENT, NULL, NULL);
/****************************************************/	

	if(gDsmCameraReq.state == DSM_CAMERA_CAPTURED)
		gDsmCameraReq.state = DSM_CAMERA_PAUSED;
	
	if (internalCovered == MR_FALSE)
	{
		if((mr_app_get_state() == DSM_RUN))
		{		
			mr_app_pause();
		}
	}
	else
	{
		internalCovered = MR_FALSE;
	}	

	/* __MMI_DSM_NEW_JSKY__ begin support camera */
	mmi_frm_set_curr_scr_blt_mode(MMI_FRM_SCR_BLT_PRIMITIVE);
	/* __MMI_DSM_NEW_JSKY__ end support camera */

	// SKY_MULTIMEDIA 修改打开camera交叉事件偶现花屏问题
	mdi_camera_preview_stop();
	
	mdi_camera_power_off();

	/* __MMI_DSM_NEW_JSKY__ begin support camera */
	if (!gDsmCameraReq.is_from_jsky)
	{
		gdi_layer_get_base_handle(&baseHandle);
		gdi_layer_push_and_set_active(baseHandle);
		gdi_layer_clear(GDI_COLOR_BLACK);
		gdi_layer_pop_and_restore_active();
	}
	/* __MMI_DSM_NEW_JSKY__ end support camera */
	dsm_uninit_camera_tmp_info();
	

	/* resume alignment timer */
	UI_enable_alignment_timers();

	/* resume LED patten */
	//GpioCtrlReq(MMIAPI_DEV_CTRL_RELEASE);

	/* let MMI can sleep */
	//TurnOffBacklight();

	/* enable multi-layer */
	gdi_layer_multi_layer_disable();	

	dsm_camera_free_preview_resource();
	dsm_camera_free_resource();
	entry_full_screen();
	mdi_audio_resume_background_play();
}

#if (MTK_VERSION >= 0x10A1032)
#define MENU_DSM_CAMERA SCR_DSM_DUMMY_199
#endif

static void dsmEntryCamera(void)
{	
	extern U16 GetCurKeypadTone(void);
	extern void mmi_frm_kbd_set_tone_state(mmi_frm_kbd_tone_state_enum state);
	
	internalCovered = MR_TRUE;
/* __MMI_DSM_NEW_JSKY__ begin support camera */
#if defined(__MMI_SCREEN_ROTATE__)
	if (current_screen_rotation != MMI_FRM_SCREEN_ROTATE_0)
	{
		mmi_frm_enable_clear_rotated_screen(MMI_FALSE);
	}
#endif
/* __MMI_DSM_NEW_JSKY__ end support camera */
	mr_app_EntryNewScreen(MENU_DSM_CAMERA, dsmExitCamera, dsmEntryCamera, MMI_FRM_FULL_SCRN);
	gDsmCameraReq.camera_handler = MENU_DSM_CAMERA;
	internalCovered = MR_FALSE;
	
	mr_app_setup_dsm_screen_event_hdlr();
	entry_full_screen();

/* __MMI_DSM_NEW_JSKY__ begin support camera */
#if defined(__MMI_SCREEN_ROTATE__)
	if (current_screen_rotation != MMI_FRM_SCREEN_ROTATE_0)
	{
		mmi_frm_screen_rotate((mmi_frm_screen_rotate_enum)current_screen_rotation);
	}
#endif
/* __MMI_DSM_NEW_JSKY__ end support camera */

	gdi_layer_reset_clip();
	gdi_layer_reset_text_clip();
	
	mdi_audio_suspend_background_play();

	/* __MMI_DSM_NEW_JSKY__ begin support camera */
	if (!gDsmCameraReq.is_from_jsky)
	{
		gdi_layer_clear(GDI_COLOR_BLACK);
	}
	/* __MMI_DSM_NEW_JSKY__ end support camera */

	/* stop MMI sleep */
	//TurnOnBacklight(0);

	/* force all playing keypad tone off */
	//AudioStopReq(GetCurKeypadTone());

	/* disable key pad tone */
	mmi_frm_kbd_set_tone_state(MMI_KEY_TONE_DISABLED);

	/* disalbe align timer  */
	UI_disable_alignment_timers();

	/* stop LED patten */
	//GpioCtrlReq(MMIAPI_DEV_CTRL_GET);

	/* enable multi-layer */
	gdi_layer_multi_layer_enable();	
	
	/*********** init layer var ************/
	/* get mainlcd based layer handle */

	//mmi_camera_load_setting();
	
	gdi_layer_get_base_handle(&gDsmCameraReq.base_layer);
	
	dsm_camera_create_resource();

	dsm_camera_preview_set_blt_layer(FALSE);
	dsm_init_camera_tmp_info();
		
	if (mr_app_get_state() == DSM_PAUSE)
	{
		mr_app_resume();
		mr_event(MR_OSD_EVENT,(int32)gDsmCameraReq.osd_layer_buffer,NULL);
	}
	
	if((gDsmCameraReq.state != DSM_CAMERA_PAUSED)&&(gDsmCameraReq.state != DSM_CAMERA_CAPTURED))
	{
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
		gDsmCameraReq.last_error = mdi_camera_power_on(0);
#else
		gDsmCameraReq.last_error = mdi_camera_power_on();
#endif		
		dsm_entry_preview();
	}
}

#endif


int32 mr_camera_destroy(int32 param)
{
#if (defined(MT6223P)||defined(MT6223)||defined(MT6223P_S00))
	return MR_IGNORE;
#else
	if(gDsmCameraReq.state > DSM_CAMERA_UNINIT)
	{
		if(param == gDsmCameraReq.camera_handler)
		{
#if 0		
			if(GetActiveScreenId() == param)
			{
				gDsmCameraReq.state = DSM_CAMERA_UNINIT;
				GoBackHistory();
			}
			else
			{
				DeleteScreenIfPresent(param);
			}
#endif
			/* __MMI_DSM_NEW_JSKY__ begin support camera */
			internalCovered = MR_TRUE;
			gDsmCameraReq.state = DSM_CAMERA_UNINIT;
			/* __MMI_DSM_NEW_JSKY__ end support camera */
			mmi_frm_scrn_close(mr_app_get_group_id(), param);
			
			dsm_unInit_camera();
			return MR_SUCCESS;
		}
	}
	return MR_FAILED;
#endif	
}


int32 mr_camera_get_preview_data(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	if(output == NULL||output_len == NULL)
		return MR_FAILED;
	
	if(dsm_get_preview_data(&dsmLayerInfo) == MR_SUCCESS)
	{
		*output = (uint8*)&dsmLayerInfo;
		*output_len = sizeof(dsmLayerInfo);
		return MR_SUCCESS;
	}
	else
	{
		return MR_FAILED;
	}
}


int32 mr_camera_create(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	mr_camera_launch_info_t *pReq = (mr_camera_launch_info_t*)input;
	
	if(input_len < sizeof(mr_camera_launch_info_t)||input == NULL||output == NULL||output_len == NULL)
		return MR_FAILED;

	if(dsm_create_camera(pReq, &g_mr_common_rsp.p1,&g_mr_common_rsp.p2)== MR_SUCCESS)
	{
		*output = (uint8 *)&g_mr_common_rsp;
		*output_len = 8;
		return MR_SUCCESS;
	}
	else
	{
		return MR_FAILED;
	}
}


int32 mr_camera_capture(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	if(input == NULL||output == NULL||output_len == NULL)
		return MR_FAILED;
	
	if(dsm_capture(*input, &g_mr_common_rsp.p1, &g_mr_common_rsp.p2) == MR_SUCCESS)
	{
		*output = (uint8 *)&g_mr_common_rsp;
		*output_len = 8;
		return MR_SUCCESS;
	}
	else
	{
		return MR_FAILED;
	}
}


////////////////////////////////////////////////////////////////////////////////////////////////////////

static void dsm_voice_record_callback(mdi_result ret)
{
	int32 p1;
	
	switch(ret)
	{
		case MED_RES_DISC_FULL:
			p1 = DSM_ERROR_NO_SPACE;
			break;
		case MED_RES_TERMINATED:
			p1 = DSM_ERROR_TERMINATED;
			if(s_dsm_vr_stream_started){
				char asciiPath[32] ={0};
				U16 fullpathname[32] ={0};
				sprintf(asciiPath,"%c:\\dummy.pcm",MMI_CARD_DRV);
				mmi_asc_to_ucs2((S8 *) fullpathname,( S8 *) asciiPath);
				FS_Delete((const WCHAR *)fullpathname);
				s_dsm_vr_stream_started = FALSE;
			}
		default:
			p1 = DSM_ERROR_UNKNOW;
	}
	
	mr_event(MR_ERROR_EVENT,p1,0);
}


int32 mr_voice_record_start(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	U16 fullpathname[DSM_MAX_FILE_LEN] = {0};
	int32 ret;
	U8 format;
	mr_voice_record_req_info_t *req = (mr_voice_record_req_info_t *)input;

	if(input_len < sizeof(mr_voice_record_req_info_t))
		return MR_FAILED;
	
	/*if(req== NULL||req->src == NULL)
		return MR_FAILED;
	
	if(req->src_type == SRC_STREAM)
		return MR_IGNORE;
	*/
	if(req == NULL)
		return MR_FAILED;
	
	switch(req->format)
	{
		case MR_SOUND_WAV:
			format = MDI_FORMAT_WAV;
			break;
		case MR_SOUND_PCM:
			format = MDI_FORMAT_PCM_8K;
			break;
		case MR_SOUND_AMR:
			format = MDI_FORMAT_AMR;
			break;
		case MR_SOUND_AMR_WB:
			format = MDI_FORMAT_AMR_WB;
			break;
		default:
			return MR_IGNORE;
			break;
	}

	memset(fullpathname,0,sizeof(fullpathname));
	s_dsm_vr_stream_started = FALSE;
	
	if(req->src_type == SRC_STREAM)
	{
		char asciiPath[32] ={0};
		sprintf(asciiPath,"%c:\\dummy.pcm",MMI_CARD_DRV);
		mmi_asc_to_ucs2((S8 *) fullpathname,( S8 *) asciiPath);
		FS_Delete((const WCHAR *)fullpathname);
		
		ret = mdi_audio_start_record((void *)fullpathname, format,
#if (MTK_VERSION >= 0x0808)
										QUALITY_LOW,
#endif
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
										dsm_voice_record_callback, NULL);
#else
										NULL, dsm_voice_record_callback);
#endif										
		if(ret == MDI_AUDIO_SUCCESS){
			s_dsm_vr_stream_started = TRUE;
		}
	}
	else
	{
	ret = mdi_audio_start_record((void *)mr_fs_get_filename((char *)fullpathname,req->src),format,
#if (MTK_VERSION >= 0x0736)//0x0808
		QUALITY_LOW,
#endif
#if (__MR_CFG_VAR_MTK_VERSION__ >= 0x11A1112)
		dsm_voice_record_callback, NULL);
#else
		NULL, dsm_voice_record_callback);
#endif		
	}

	if(ret == MDI_AUDIO_SUCCESS)		
		return MR_SUCCESS;
	else
		return MR_FAILED;
}

int32 mr_voice_record_pause(int32 param)
{
	if( mdi_audio_get_state() !=MDI_AUDIO_RECORD)
		return MR_FAILED;
	if(mdi_audio_pause(NULL,NULL)== MDI_AUDIO_SUCCESS)
		return MR_SUCCESS;
	else
		return MR_FAILED;
}

int32 mr_voice_record_resume(int32 param)
{
	if( mdi_audio_get_state() !=MDI_AUDIO_RECORD_PAUSED)
		return MR_FAILED;
	if(mdi_audio_resume(NULL,NULL) == MDI_AUDIO_SUCCESS)
		return MR_SUCCESS;
	else
		return MR_FAILED;
}

int32 mr_voice_record_stop(int32 param)
{
	U16 stat = mdi_audio_get_state();
	int32 ret = MR_FAILED;
	
	if(stat == MDI_AUDIO_RECORD_PAUSED||stat == MDI_AUDIO_RECORD)
	{
		if(mdi_audio_stop_record() == MDI_AUDIO_SUCCESS)
			ret = MR_SUCCESS;
		else
			ret = MR_FAILED;
	}
	else
	{
		ret = MR_FAILED;
	}
	
	if(s_dsm_vr_stream_started){
		char asciiPath[32] ={0};
		U16 fullpathname[32] ={0};
		sprintf(asciiPath,"%c:\\dummy.pcm",MMI_CARD_DRV);
		mmi_asc_to_ucs2((S8 *) fullpathname,( S8 *) asciiPath);
		
		FS_Delete((const WCHAR *)fullpathname);
		s_dsm_vr_stream_started = FALSE;
	}
	return ret;
}


static void mr_voice_record_read_done(uint8* output, int32 output_len)
{
	if(dsm_voice_record_stream_isStart() && output_len > 0)
	{
		Media_ReadDataDone((unsigned int)output_len>>1);//byte to word
	}
}


int32 mr_voice_record_get_buffer(uint8* input, int32 input_len, uint8** output, int32* output_len, MR_PLAT_EX_CB *cb)
{
	uint32 uBufLen = 0;         
	if(output && output_len && cb && dsm_voice_record_stream_isStart())
	{
		Media_GetReadBuffer((kal_uint16**)output, (kal_uint32*)&uBufLen);
		*output_len = uBufLen<<1;// word to byte
		*cb = mr_voice_record_read_done;
		return MR_SUCCESS;	
	}
	return MR_FAILED;

}


char dsm_voice_record_stream_isStart(void)
{
	return s_dsm_vr_stream_started;
}

#ifdef __MMI_DSM_NEW_JSKY__
S32 dsm_get_audio_time_ms(void)
{
	U32 time = 0;
	
	if(gDsmAudio.state < MR_MEDIA_LOADED)
		return -1;
	
	if(gDsmAudio.load_type == MR_MEDIA_FILE_LOAD)
	{
		if(mdi_audio_get_duration(gDsmAudio.filename,&time) == MDI_AUDIO_SUCCESS)
		{
#if (MTK_VERSION == 0x05c)
			if(gDsmAudio.media_format == MDI_FORMAT_M4A)
				return time/2;
			else
#endif				
				return time;
		}
		else
		{
			return -1;
		}
	}
	else
	{
		return -1;
	}
}
#endif /*__MMI_DSM_NEW_JSKY__*/

#endif

